package com.lwl.office.dcconverter.style;

import cn.hutool.core.util.StrUtil;
import com.lwl.office.dcconverter.cons.DCDocumentEnum;
import com.lwl.office.webeditor.tools.ConvertUtil;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;

/**
 * run节点的样式
 */
@Data
public class DCRunStyle extends AbsDCStyle{
    public DCRunStyle(DCDocumentEnum type) {
        super(type);
    }

    /**
     * 加粗
     * html： strong节点或style:font-weight:bold;
     * word: w:b
     */
    private boolean bold;

    private String boldHtmlStyle(){
        return bold?" font-weight: bold;":"";
    }
    /**
     * 下划线
     * html text-decoration: underline;
     * word w:u
     */
    private boolean underline;
    /**
     * 中划线
     * html text-decoration: line-through;
     * word w:strike
     */
    private boolean lineThrough;
    private String textDecorationHtml(){
        StringBuilder stb = new StringBuilder();
        if(lineThrough|| underline){
            stb.append(" text-decoration:");
            if(underline){
                stb.append(" underline");
            }
            if(lineThrough){
                stb.append(" line-through");
            }
        }
        return stb.toString();
    }
    /**
     *字体大小
     * html font-size: 14px;
     * word w:szCs w:val="21"
     */
    private String fontSize;
    private String fontSizeHtmlStyle(){
        return StrUtil.isNotEmpty(fontSize)?
                (" font-size: "+(getType().equals(DCDocumentEnum.HTML)?fontSize:((Integer.parseInt(fontSize)/1.5)+"px"))+";")
                :"";
    }
    /**
     * 字体
     * html font-family: 楷体, 楷体_GB2312, SimKai;
     * word w:rFonts w:ascii="楷体" w:eastAsia="楷体"
     */
    private String fontFamily;
    private String fontFamilyHtmlStyle(){
        return StrUtil.isNotEmpty(fontFamily)?
                (" font-family: "+fontFamily+";")
                :"";
    }
    /**
     * 字体颜色
     * html color: #FF0000;
     * word w:color w:val="FF0000"
     */
    private String color;
    private String colorHtmlStyle(){
        return StrUtil.isNotEmpty(color)?
                (" color: "+(getType().equals(DCDocumentEnum.HTML)?"":"#")+color+";")
                :"";
    }
    /**
     * 背景颜色
     * html background-color: #C4BD97;
     * word w:shd w:val="clear" w:color="auto" w:fill="C4BD97"
     */
    private String bgColor;
    private String bgColorHtmlStyle(){
        return StrUtil.isNotEmpty(bgColor)?
                (" background-color: "+(getType().equals(DCDocumentEnum.HTML)?"":"#")+bgColor+";")
                :"";
    }
    /**
     * 外边框
     * html border: 1px solid #000000;
     * word w:bdr w:val="single" w:sz="4" w:space="0" w:color="auto"
     */
    private boolean border;
    private String borderHtmlStyle(){
        return border?" border: 1px solid #000000;":"";
    }
    /**
     * 斜体
     * html em节点或    font-style: italic;
     * word w:i
     */
    private boolean italic;
    private String italicHtmlStyle(){
        return italic?" font-style: italic;":"";
    }
    /**
     * 上标
     * html sub节点 或vertical-align: sub;
     * word w:vertAlign w:val="superscript"
     */
    private boolean superscript;
    private String superscriptHtmlStyle(){
        return superscript?" vertical-align: super;":"";
    }
    /**
     * 下标
     * html sup节点 或 vertical-align: super;
     * word w:vertAlign w:val="subscript"
     */
    private boolean subscript;
    private String subscriptHtmlStyle(){
        return subscript?" vertical-align: sub;":"";
    }
    @Override
    public String toHtmlStyle() {
        StringBuilder stb = new StringBuilder();
        stb.append(" style=\"");
        stb.append(bgColorHtmlStyle());
        stb.append(boldHtmlStyle());
        stb.append(borderHtmlStyle());
        stb.append(colorHtmlStyle());
        stb.append(fontFamilyHtmlStyle());
        stb.append(textDecorationHtml());
        stb.append(fontSizeHtmlStyle());
        stb.append(italicHtmlStyle());
        stb.append(subscriptHtmlStyle());
        stb.append(superscriptHtmlStyle());
        stb.append(" \"");
        return stb.toString();
    }

    @Override
    public void setWordStyle(Object obj) {
        XWPFRun run = (XWPFRun) obj;
        CTR ctr = run.getCTR();
        CTRPr ctrPr = ctr.addNewRPr();
        if(bold){
            ctrPr.addNewB();
        }
        if(underline){
            CTUnderline ctUnderline = ctrPr.addNewU();
            ctUnderline.setVal(STUnderline.SINGLE);
        }
        if(lineThrough){
            ctrPr.addNewStrike();
        }
        if(StrUtil.isNotEmpty(fontSize)){
            if(getType().equals(DCDocumentEnum.HTML)){
                CTHpsMeasure ctHpsMeasure = ctrPr.addNewSzCs();
                ctHpsMeasure.setVal(ConvertUtil.toWordFontSize(fontSize));
            }
        }
        if(StrUtil.isNotEmpty(fontFamily)){
            CTFonts ctFonts = ctrPr.addNewRFonts();
            ctFonts.setAscii(fontFamily);
            ctFonts.setEastAsia(fontFamily);
            ctFonts.setHAnsi(fontFamily);
        }

        if(StrUtil.isNotEmpty(color)){
            CTColor ctColor = ctrPr.addNewColor();
            ctColor.setVal("#"+color);
        }

        if(StrUtil.isNotEmpty(bgColor)){
            CTShd ctShd = ctrPr.addNewShd();
            ctShd.setVal(STShd.CLEAR);
            ctShd.setColor("auto");
            ctShd.setFill(bgColor);
        }
        if(border){
            CTBorder ctBorder = ctrPr.addNewBdr();
            ctBorder.setVal(STBorder.SINGLE);
            ctBorder.setSz(BigInteger.valueOf(4));
            ctBorder.setColor("auto");
        }
        if(italic){
            ctrPr.addNewI();
        }
        if(subscript){
            CTVerticalAlignRun ctVerticalAlignRun = ctrPr.addNewVertAlign();
            ctVerticalAlignRun.setVal(STVerticalAlignRun.SUBSCRIPT);
        }
        if(superscript){
            CTVerticalAlignRun ctVerticalAlignRun = ctrPr.addNewVertAlign();
            ctVerticalAlignRun.setVal(STVerticalAlignRun.SUPERSCRIPT);
        }
    }
}
